package com.xquant.databasemanagement.controller.panel.center;

import com.xquant.databasemanagement.bean.table.ColumnBean;
import com.xquant.databasemanagement.bean.table.IndexesBean;
import com.xquant.databasemanagement.bean.table.PrimaryKeyBean;
import com.xquant.databasemanagement.bean.table.TableBean;
import com.xquant.databasemanagement.controller.FormController;
import com.xquant.databasemanagement.controller.windows.ColumnWindowController;
import com.xquant.databasemanagement.controller.windows.IndexesWindowController;
import com.xquant.databasemanagement.controller.windows.PrimaryKeyWindowController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;


public class TablePanelController implements Initializable, FormController {

    @FXML
    private TextArea tableAnnotation;
    @FXML
    private TextField tableSpaces;
    @FXML
    private TextField tableName;
    @FXML
    private TableView<PrimaryKeyBean> primaryKeyTable;

    @FXML
    private TableView<IndexesBean> indexesTable;

    @FXML
    private TableView<ColumnBean> columnTable;

    @FXML
    private VBox tablePanel;

    @FXML
    private VBox columnWindow;

    @FXML
    private VBox indexesWindow;

    @FXML
    private VBox primaryKeyWindow;

    private Scene columnWindowScene;

    private Scene indexesWindowScene;

    private  Scene principalKeyWindowScene;

    @FXML
    private ColumnWindowController columnWindowController;

    @FXML
    private IndexesWindowController indexesWindowController;

    @FXML
    private PrimaryKeyWindowController primaryKeyWindowController;

    private TableBean tableBean = null;

    private TreeItem treeItem = null;

    private TreeView treeView;



    @Override
    public void initialize(URL location, ResourceBundle resources) {
        columnWindowScene = new Scene(columnWindow);
        indexesWindowScene = new Scene(indexesWindow);
        principalKeyWindowScene = new Scene(primaryKeyWindow);

    }

    @FXML
    private void clickAddPrimaryKeyButton(ActionEvent event){
        primaryKeyWindowController.clear();
        primaryKeyWindowController.initColumnNames(columnTable.getItems());
        showChildWindow(principalKeyWindowScene);
    }


    @FXML
    private void clickDeletePrimaryKeyButton(ActionEvent event){
        if (primaryKeyTable.getSelectionModel().getSelectedIndex()!=-1) {
            primaryKeyTable.getItems().remove(primaryKeyTable.getSelectionModel().getSelectedIndex());
        }
    }

    @FXML
    private void clickAddColumnButton(ActionEvent event){
        columnWindowController.clear();
        showChildWindow(columnWindowScene);
    }
    @FXML
    private void clickDeleteColumnButton(ActionEvent event){

        if (columnTable.getSelectionModel().getSelectedIndex()!=-1) {
            columnTable.getItems().remove(columnTable.getSelectionModel().getSelectedIndex());
        }

    }
    @FXML
    private void clickAddIndexesButton(ActionEvent event){
        indexesWindowController.clear();
        indexesWindowController.initColumnNames(columnTable.getItems());
        showChildWindow(indexesWindowScene);
    }
    @FXML
    private void clickDeleteIndexesButton(ActionEvent event){
        if (indexesTable.getSelectionModel().getSelectedIndex()!=-1) {
            indexesTable.getItems().remove(indexesTable.getSelectionModel().getSelectedIndex());
        }

    }


    private void showChildWindow( Scene secondScene){
        // New window (Stage)
        Stage newWindow = new Stage();
        newWindow.setScene(secondScene);

        // Specifies the modality for new window.
        newWindow.initModality(Modality.WINDOW_MODAL);

        // Specifies the owner Window (parent) for new window
        newWindow.initOwner(tablePanel.getParent().getScene().getWindow());
        newWindow.show();
    }


    @FXML
    private void columnTableClicked(MouseEvent mouseEvent) {
        if (mouseEvent.getClickCount()==2){
            if (columnTable.getSelectionModel().getSelectedIndex()!=-1) {
                columnWindowController.clear();
                columnWindowController.setColumnBean(
                        columnTable.getSelectionModel().getSelectedItem());
                showChildWindow(columnWindowScene);
            }
        }
    }

    public void primaryKeyTableClicked(MouseEvent mouseEvent) {
        if (mouseEvent.getClickCount()==2){
            if (primaryKeyTable.getSelectionModel().getSelectedIndex()!=-1) {
                primaryKeyWindowController.clear();
                primaryKeyWindowController.initColumnNames(columnTable.getItems());
                primaryKeyWindowController.setPrimaryKeyBean(primaryKeyTable.getSelectionModel().getSelectedItem());
                showChildWindow(principalKeyWindowScene);
            }
        }
    }

    public void indexesTableClicked(MouseEvent mouseEvent) {
        if (mouseEvent.getClickCount()==2){
            if (indexesTable.getSelectionModel().getSelectedIndex()!=-1) {
                indexesWindowController.clear();
                indexesWindowController.initColumnNames(columnTable.getItems());
                indexesWindowController.setIndexesBean(indexesTable.getSelectionModel().getSelectedItem());
                showChildWindow(indexesWindowScene);
            }
        }
    }

    @FXML
    private void actionSaveTableButton(ActionEvent event) {
        if (isValid()) {
            setTableBean();
            if (this.treeItem != null) {
                treeItem.getChildren().add(new TreeItem(this.tableBean));
            } else
                treeView.refresh();
            clear();
            ((BorderPane) tablePanel.getParent()).setCenter(null);
        }
    }

    @Override
    public void clear(){
        tableBean = null;
        treeItem = null;
        columnTable.getItems().clear();
        primaryKeyTable.getItems().clear();
        indexesTable.getItems().clear();
        tableName.clear();
        tableSpaces.clear();
        tableAnnotation.clear();
    }

    @Override
    public boolean isValid() {

        if (!tableName.getText().matches("^\\w+$")) {
            new Alert(Alert.AlertType.NONE, "表命名不正确", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        }else if (!tableSpaces.getText().matches("^\\w+$")){
            new Alert(Alert.AlertType.NONE, "表空间命名不正确", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        }else
            return true;
    }


    public void setTableBean() {
        if (this.tableBean==null)
            this.tableBean = new TableBean();

        this.tableBean.setName(tableName.getText());
        this.tableBean.setTableAnnotation(tableAnnotation.getText());
        this.tableBean.setTableSpaces(tableSpaces.getText());
        this.tableBean.setTableIndexes(new ArrayList<IndexesBean>(indexesTable.getItems()));
        this.tableBean.setTablePrimaryKeys(new ArrayList<PrimaryKeyBean>(primaryKeyTable.getItems()));
        this.tableBean.setTableColumns(new ArrayList<ColumnBean>(columnTable.getItems()));

    }

    public void setTableBean(TableBean tableBean) {
        this.tableBean = tableBean;
        tableName.setText(tableBean.getName());
        tableSpaces.setText(tableBean.getTableSpaces());
        tableAnnotation.setText(tableBean.getTableAnnotation());
        columnTable.getItems().addAll(tableBean.getTableColumns());
        indexesTable.getItems().addAll(tableBean.getTableIndexes());
        primaryKeyTable.getItems().addAll(tableBean.getTablePrimaryKeys());
    }


    public void setTreeItem(TreeItem treeItem) {
        this.treeItem = treeItem;
    }


    public void setTreeView(TreeView treeView){
        this.treeView = treeView;
    }



}
